package cn.louguanyang.carbon.utils;

import java.util.Random;

/**
 * Created by louguanyang on 15/12/4.
 */
public class StringUtils {
    private StringUtils() {
        throw new AssertionError();
    }

    public static boolean isBlank(String str) {
        return (str == null || str.trim().length() == 0);
    }

    public static boolean isEmpty(CharSequence str) {
        return (str == null || str.length() == 0);
    }
    /**
     * 邀请码组成字符:
     * 7, Q, A, X, s,
     * W, 8, c, D, e,
     * 5, R, f, V, 3,
     * b, K, t, 6, Y,
     * h, N, 4, u, J,
     * m, 9, p, L, G
     */
    private final static char[] FIRST_PART = {'7', 'Q', 'A', 'X', 's',
            'W', '8', 'c', 'D', 'e',
            '5', 'R', 'f', 'V', '3',
            'b', 'K', 't', '6', 'Y',
            'h', 'N', '4', 'u', 'J',
            'm', '9', 'p', 'L', 'G'};
    /**
     * 用于补全的字符:
     * a, Z, x, i, w,
     * C, d, E, r, F,
     * P, B, k, T, y,
     * H, n, U, j, M
     */
    private final static char[] SECOND_PART = {'a', 'Z', 'x', 'i', 'w',
            'C', 'd', 'E', 'r', 'F',
            'P', 'B', 'k', 'T', 'y',
            'H', 'n', 'U', 'j', 'M'};
    private final static int DEFAULT_CODE_LENGTH = 6;

    public static String getInvitationCode(long userId) {
        StringBuffer buffer = new StringBuffer();
        while (userId / FIRST_PART.length > 0) {
            buffer.append(FIRST_PART[(int) (userId % FIRST_PART.length) - 1]);
            userId /= FIRST_PART.length;
        }
        buffer.append(FIRST_PART[(int) (userId % FIRST_PART.length) - 1]);
        String code = buffer.reverse().toString();//倒序
        if (code.length() < DEFAULT_CODE_LENGTH) {
            Random random = new Random();
            buffer = new StringBuffer();
            for (int i = 0; i < DEFAULT_CODE_LENGTH - code.length(); i++) {
                int num = random.nextInt(SECOND_PART.length);
                buffer.append(SECOND_PART[num]);
            }
            code = buffer.toString() + code;
        }
        return code;
    }
}
